function F = companion(b,p,K);

% **********************************************************************************
%                              COMPANION MATRIX
% **********************************************************************************
% FUNCTION: companion.m 
%           Forms the Companion matrix of a VAR
%
% REQUIRES: - Inputs: b = K x(K*p+1) matrix of VAR parameters
%                             (includes the constant)
%                     p = number of lags
%                     K = number of variables in the VAR           
%
% Program by Jean Boivin and Marc Giannoni
% Program may be used provided that full credit is given to the source
% Created 11/08/01
% **********************************************************************************

F = zeros(p*K,p*K);
F(1:K,:) = b(:,2:end);		                        % Removes the constant estimate

for jj = 1:p-1;
	F(K*jj+1:(jj+1)*K,(jj-1)*K+1:K*jj) = eye(K);
end;